﻿// Upgrade NOTE: replaced 'defined _EFFECT_DISSOLVE_ALPHABLEND' with 'defined (_EFFECT_DISSOLVE_ALPHABLEND)'

// Upgrade NOTE: replaced 'defined _EFFECT_DISSOLVE_ALPHABLEND' with 'defined (_EFFECT_DISSOLVE_ALPHABLEND)'

#ifndef QTZ_EFFECTLIBRARY_SHADER_INPUT_INCLUDE
#define QTZ_EFFECTLIBRARY_SHADER_INPUT_INCLUDE


uniform sampler2D _DissolveMap; uniform float4 _DissolveMap_ST;
uniform float _EdgeSize;
uniform float4 _EdgeColor;
uniform sampler2D _NoiseMap; uniform float4 _NoiseMap_ST;
uniform float _DissolveNoiseIntensity;
uniform float _ScrollSpeedX;
uniform float _ScrollSpeedY;
uniform float _EdgeSoftness;
uniform float _EdgeIntensity;

inline fixed4 DissolveAlphaBlend(float4 finalColor,float2 uv,float dissolveThreshold)
{
	float edgeSoftFactor = _EdgeSoftness * 0.1;
	float edgeSizeFactor = _EdgeSize * 0.1;

	float _NoiseMap_var = tex2D(_NoiseMap, TRANSFORM_TEX(uv, _NoiseMap) + float2(_ScrollSpeedX, _ScrollSpeedY) * _Time.x).r;

	float _DissolveMap_var = tex2D(_DissolveMap, TRANSFORM_TEX(uv, _DissolveMap) + _DissolveNoiseIntensity * _NoiseMap_var).r;

	float dissolveFactor = _DissolveMap_var - dissolveThreshold + 0.5;

	half3 edgeCol = _EdgeColor.rgb * _EdgeIntensity;

	half3 emissive = lerp(finalColor.rgb, edgeCol, smoothstep(0.5 - edgeSoftFactor, 0.5, 1.0 - dissolveFactor + edgeSizeFactor) * _EdgeIntensity);

	float alpha = finalColor.a * smoothstep(0.5 - edgeSoftFactor, 0.5, dissolveFactor);

	finalColor = fixed4(emissive, alpha);
	return finalColor;
}

inline fixed4 DissolveAlphaTest(float4 finalColor, float2 uv, float dissolveThreshold)
{

	float2 timeScroll = float2(_ScrollSpeedX, _ScrollSpeedY) * _Time.x;
	half _NoiseValue = tex2D(_NoiseMap, TRANSFORM_TEX(uv, _NoiseMap) + timeScroll).r;
	float2 dissolveUV = _DissolveNoiseIntensity * _NoiseValue + uv;
	half _DissolveValue = tex2D(_DissolveMap, TRANSFORM_TEX(dissolveUV, _DissolveMap)).r;
	clip(_DissolveValue - dissolveThreshold);

	float edgeVar = step(_DissolveValue, (_EdgeSize + dissolveThreshold));
	finalColor.rgb = lerp(finalColor.rgb, _EdgeColor.rgb*_EdgeIntensity, edgeVar);

	return finalColor;
}

float _TimeOffset = 0.5;
float _TimeOnDuration;
float _TimeOffDuration;
float _TimeSmoothIn;
float _TimeSmoothOut;
float _NoiseAmount;

inline float PulseNoiseWaveform()
{
	float time = _Time.y + _TimeOffset;
	float fracTime = fmod(time, _TimeOnDuration + _TimeOffDuration);
	float wave = smoothstep(0, _TimeOnDuration * _TimeSmoothIn, fracTime)  * (1 - smoothstep(_TimeOnDuration * (1 - _TimeSmoothOut), _TimeOnDuration, fracTime));

	float noiseTime = time * (6.2831853f / _TimeOnDuration);
	float noiseWave = (sin(noiseTime) * (0.5f * cos(noiseTime * 0.6366f + 56.7272f) + 0.5f) + 1)*0.5;

	noiseWave = _NoiseAmount * noiseWave + (1 - _NoiseAmount);

	wave = _NoiseAmount < 0.01f ? wave : noiseWave;

	return wave;
}

#endif